<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item label="销售单类型" prop="saleType">
        <el-select v-model="dataForm.saleType" placeholder="配镜类型">
          <el-option v-for="item in saleTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="在途状态">
        <el-input v-model="inTransitIdStr">
          <el-button slot="append" @click="statusHandle">选择</el-button>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>

    <status-list v-if="statusVisible" ref="statusDialog" @refreshDataList="getStatusData"></status-list>
  </el-dialog>
</template>

<script>
  import {
    getEimsSaleTypeInTransitObj,
    addEimsSaleTypeInTransitObj,
    putEimsSaleTypeInTransitObj
  } from '@/api/eims/saletypeintransit'

  import StatusList from './StatusList'

  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        statusVisible: false,
        saleTypeOptions: [
          { value: 1, label: '隐形订做' },
          { value: 2, label: '框镜订做' },
          { value: 3, label: '隐形成品' },
          { value: 4, label: '框镜成品' },
          { value: 5, label: '辅料' }
        ],
        saleStatusOption: [],
        inTransitIdStr: '',
        dataForm: {
          id: 0,
          saleType: '',
          inTransits: [],
        },
        dataRule: {
          saleType: [{
            required: true,
            message: '销售单类型不能为空',
            trigger: 'blur'
          }],
          inTransits: [{
            required: true,
            message: '在途状态id不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    components: {
      StatusList
    },
    methods: {
      init(type) {
        this.visible = true
        this.$set(this.dataForm,'saleType',type)
        this.$set(this.dataForm,'inTransits',[])
        this.inTransitIdStr = ''
        this.$nextTick(() => {
          if (this.dataForm.saleType) {
            getEimsSaleTypeInTransitObj(this.dataForm.saleType).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.id = 111
                let list = []
                this.dataForm.inTransits.forEach(item => {
                  list.push(item.name)
                })
                this.inTransitIdStr = list.join(',')
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putEimsSaleTypeInTransitObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addEimsSaleTypeInTransitObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      statusHandle(){
        this.statusVisible = true
        this.$nextTick(() => {
          this.$refs.statusDialog.init(this.dataForm.inTransits)
        })
      },
      getStatusData(data){
        let list = []
        data.forEach(item => {
          list.push(item.name)
        })
        this.$set(this.dataForm,'inTransits',data)
        this.inTransitIdStr = list.join(',')
      }
    }
  }
</script>
